package com.zhibang.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redis配置类
 *
 * @author: ZXT
 * @date: 2024-12-08 14:13
 */
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String,Object>redisTemplate(RedisConnectionFactory rcf){
        RedisTemplate<String,Object> rt=new RedisTemplate<>();
        rt.setConnectionFactory(rcf);
        //创建序列化器 默认是jdk序列化器
        //1.可读性差,需要实现序列化接口
        JdkSerializationRedisSerializer jdk = new JdkSerializationRedisSerializer();
        //2.字符串序列化器，只能支持string
        StringRedisSerializer string = new StringRedisSerializer();
        //3.jackson序列化器 不需要实现序列化 可读性高
        GenericJackson2JsonRedisSerializer jack=new GenericJackson2JsonRedisSerializer();
        //设置序列化器
        rt.setKeySerializer(string);//键
        rt.setHashKeySerializer(string);

        rt.setValueSerializer(jack);//值
        rt.setHashValueSerializer(jack);
        return rt;
    }
}

